package com.xhui.vimarket.zkplus.zul.attr.rule;

import java.util.ArrayList;

import org.zkoss.zk.ui.Component;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.util.crm.WeakPassword;
import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attr.bean.com.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusil;
import com.xhui.vimarket.zkplus.zul.com.xhui.vimarket.zkplusttrBaseComp;
import com.xhui.vimarket.zkplus.zul.attr.util.RegExpValidator;

/**
 * AttrValueLengthRule.
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-3-23
 * @功能说明：
 * 
 */
public final class AttrValueRexRule {
    
    /**
     * SPLIT.
     */
    public static final String SPLIT = "_SPLIT_";
    /**
     * 不能为弱密码.
     */
    public static final String RULE_NOTWEAK = "notWeak";
    
    
    /**
     * 构造函数.
     * 
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    private AttrValueRexRule() {
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judge(final ArrayList<AttrInst> valList, final Component rootComp) {
        final RetVO retVo = RetVO.newInstance(Constants.RET_TRUE, "");
        if (valList != null && valList.size() > 0) { // 默认的判断方式
            final String sSetRex = AttrCompUtil.getCompAttrStr(rootComp,
                AttrBaseComp.COMP_VALUE_REX);
            if (sSetRex != null) { // 有配置，需判断
                final AttrInst inst = valList.get(0);
                if (inst.getParam1() != null) {
                    final String[] arrayRex = sSetRex.split(AttrValueRexRule.SPLIT);
                    if (arrayRex != null && arrayRex.length > 0) {
                        for (int i = 0; i < arrayRex.length; i++) {
                            if (!AttrValueRexRule.match(arrayRex[i], inst.getParam1())) {
                                retVo.setResult(Constants.RET_FALSE);
                                retVo.setRetMsg(AttrCompUtil.getCompName(rootComp) + "值无法匹配表达式:"
                                    + arrayRex[i]);
                                break;
                            }
                        }
                    }
                }
            }
            final String checkParam = AttrCompUtil.getCompAttrStr(rootComp,
                AttrBaseComp.COMP_PARAM1);
                if (checkParam != null && checkParam.contains("%" + AttrValueRexRule.RULE_NOTWEAK + "%")) { // 需要判断
                    AttrInst inst = valList.get(0);
                    if (WeakPassword.isWeak(inst.getParam1())) {
                        retVo.setResult(Constants.RET_FALSE);
                        retVo.setRetMsg(retVo.getRetMsg() + "密码设置过于简单,请重新设置！");
                    }
                }
        }
        return retVo;
    }
    
    /**
     * judge.
     * 
     * @param valList ArrayList<AttrInst>
     * @param rootComp Component
     * @return RetVO
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static RetVO judgePassword(final ArrayList<AttrInst> valList, final Component rootComp) {
        return AttrValueRexRule.judge(valList, rootComp);
    }
    
    /** 
     * 验证输入字符串.
     * @param regex 正则 
     * @param str 待验证的字符串 
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b> 
     */
    public static boolean match(final String regex, final String str) {
        return RegExpValidator.match(regex, str);
    }
}
